package com.hrp.service;

import com.hrp.common.ChinesePinyinUtil;
import com.hrp.domain.Jxkhzq;
import com.hrp.domain.Jxksdmk;
import com.hrp.domain.Yydic;
import com.hrp.hrpdao.CommonDao;
import com.hrp.hrpdao.JxksdmkDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/10 0010.
 */
@Service
public class JxksdmkService {

    @Autowired
    private JxksdmkDao jxksdmkDao;

    @Autowired
    private CommonDao commonDao;


    public Map<String, Object> getJxksdmByPage(Integer kslb,Integer page,Integer rows){
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
        try{
            Integer total = jxksdmkDao.getTotalCount();
            //List<Jxkhzq> list = jxkhzqDao.getJxkhzqByPage(10,20);

            List<Integer> kslbList = new ArrayList<>();
            if("-1".equals(kslb.toString())){
                List<Yydic> dicList = commonDao.getYydicBylbdm("1");
                for(Yydic dic:dicList){
                    kslbList.add(dic.getId());
                }
            }else {
                kslbList.add(kslb);
            }
            Integer startIndex = (page-1)*rows;
            Integer endIndex = page*rows;
            List<Integer> listStr = new ArrayList<>();
            for(Integer i=(startIndex+1);i<=endIndex;i++){
                listStr.add(i);
            }
            List<Jxksdmk> list = jxksdmkDao.getJxksdmByPage(kslbList,listStr);
            jsonMap.put("total", total);//total键 存放总记录数，必须的
            jsonMap.put("rows", list);//rows键 存放每页记录 list
            return jsonMap;
        }catch (Exception e) {
            e.printStackTrace();
            //logger.info("获取分页账期数据失败");
            return jsonMap;
        }
    }

    public String getJxksdmByKslb(Integer kslb){
        String jxksdm = jxksdmkDao.getJxksdmByKslb(kslb);
        if( jxksdm==null || jxksdm.equals("")){
            return kslb.toString() +"001";
        }else{
            Integer maxksdm = Integer.parseInt(jxksdm)+1;
            return maxksdm.toString();
        }
    }

    public void saveJxksData(Jxksdmk jxksdmk){
        jxksdmk.setPy(ChinesePinyinUtil.getPinYinHeadChar(jxksdmk.getJxksmc()));
        Jxksdmk jxksdmk1 = jxksdmkDao.getJxksdmByJxksdm(jxksdmk.getJxksdm());
        if(jxksdmk1==null){
            jxksdmkDao.saveJxksData(jxksdmk);
        }else{
            jxksdmkDao.updateJxksData(jxksdmk);
        }
    }

}
